import { existsSync, mkdirSync, renameSync } from 'fs';
import pino from 'pino';

if (!existsSync('logs')) mkdirSync('logs');
if (existsSync('logs/latest.log'))
  renameSync('logs/latest.log', 'logs/last.log');

const logLevel = process.env.LOG_LEVEL ?? 'info';
export const logger = pino(
  {
    level: logLevel,
  },
  pino.multistream([
    {
      level: logLevel,
      stream: pino.destination(),
    },
    {
      level: 'info',
      stream: pino.destination({
        dest: './logs/latest.log',
        sync: false,
      }),
    },
    {
      level: 'warn',
      stream: pino.destination({
        dest: './logs/error.log',
        sync: false,
      }),
    },
  ]),
);
export default logger;
